/*
// Exepack.NET
// http://www.codeplex.com/exepack
//
// Loader
// Written by Y [10-01-09]
// Copyright (c) 2008-2010 Alexey Yakovlev
//
// csc loader.cs 
//  /res:YXBwbGliLCB2ZXJzaW9uPTAuMC4wLjAsIGN1bHR1cmU9bmV1dHJhbCwgcHVibGlja2V5dG9rZW49bnVsbA==
//  /res:YXBwLCB2ZXJzaW9uPTAuMC4wLjAsIGN1bHR1cmU9bmV1dHJhbCwgcHVibGlja2V5dG9rZW49bnVsbA==
*/

using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.IO.Compression;

static class Loader
{
	[STAThread]
	static int Main(string[] args)
	{
		AppDomain.CurrentDomain.AssemblyResolve += ExtractAssembly;
		return AppDomain.CurrentDomain.ExecuteAssemblyByName(
			new AssemblyName("app, version=0.0.0.0, culture=neutral, publickeytoken=null"), null, args);
	}

	static Assembly ExtractAssembly(object sender, ResolveEventArgs args)
	{
		return GetResourceAssembly(Convert.ToBase64String(Encoding.Default.GetBytes(args.Name.ToLowerInvariant())));
	}

	static Assembly GetResourceAssembly(string name)
	{
		using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
		{
			if (stream == null)
				return null;

			using (BinaryReader br = new BinaryReader(stream))
			{
				int length = br.ReadInt32();

				using (Stream ds = new DeflateStream(stream, CompressionMode.Decompress))
					return Assembly.Load(new BinaryReader(ds).ReadBytes(length));
			}
		}
	}
}
